package com.gongpingjialoan.activity.tool;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.tool.InsuranceAdapter;
import com.gongpingjialoan.adapter.tool.RiskCovrAdapter;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * 车辆出险购险记录查询          wang
 * @author Administrator
 *
 */
public class InsuranceResultActivity extends BaseActivity
{
    
    ListView listV;
    
    LoadingDialog loadingDialog;
    
    private List<Map<String, String>> recordList;
    
    private List<Map<String, String>> riskCoverList;
    
    private InsuranceAdapter insurAdapter;
    
    private RiskCovrAdapter riskAdapter;
    
    RadioGroup radioG;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance_result);
        initView();
        
    }
    
    private void initView()
    {
        setTitle("出险购险查询");
        radioG = (RadioGroup)findViewById(R.id.tab);
        radioG.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            
            @Override
            public void onCheckedChanged(RadioGroup arg0, int checkId)
            {
                if (checkId == R.id.radio_left)
                {
                    listV.setAdapter(insurAdapter);
                }
                else
                {
                    listV.setAdapter(riskAdapter);
                }
            }
        });
        
        listV = (ListView)findViewById(R.id.listview);
        
        listV.setOnItemClickListener(new OnItemClickListener()
        {
            
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                if (radioG.getCheckedRadioButtonId() == R.id.radio_left)
                {
                    bundle.putSerializable("record", (Serializable)recordList.get(arg2));
                    intent.putExtras(bundle);
                    intent.setClass(InsuranceResultActivity.this, InsuranceRecordDetailActivity.class);
                }
                else
                {
                    bundle.putSerializable("record", (Serializable)riskCoverList.get(arg2));
                    intent.putExtras(bundle);
                    intent.setClass(InsuranceResultActivity.this, RisksCoverDetailActivity.class);
                }
                startActivity(intent);
            }
        });
        
        insurAdapter = new InsuranceAdapter(InsuranceResultActivity.this);
        listV.setAdapter(insurAdapter);
        
        riskAdapter = new RiskCovrAdapter(InsuranceResultActivity.this);
        getInsuranceInfo();
    }
    
    private void getInsuranceInfo()
    {
        // progressDialog = ProgressDialog.show(InsuranceActivity.this, "请稍等...","正在提交请求...", true);
        Intent it = getIntent();
        String vin = it.getStringExtra("vin");
        String engine = it.getStringExtra("engine");
        loadingDialog = new LoadingDialog(this);
        loadingDialog.show();
        String url = "/mobile/more/insurance-query/";

        NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener()
        {
            
            @Override
            public void onDataJsonError(String errorMessage)
            {
                // progressDialog.dismiss();
                // Toast.makeText(getApplicationContext(), errorMessage,Toast.LENGTH_SHORT).show();
                loadingDialog.dismiss();
                showTips(4, errorMessage);
            }
            
            @Override
            public void onDataJsonUpdate(JSONObject json)
            {
                // progressDialog.dismiss();
                loadingDialog.dismiss();
                recordList = new ArrayList<Map<String, String>>();
                riskCoverList = new ArrayList<Map<String, String>>();
                Map<String, String> recordMap;
                try
                {
                    JSONArray jsonArray = json.getJSONArray("records");
                    JSONObject jsonRecord;
                    for (int i = 0; i < jsonArray.length(); i++)
                    {
                        jsonRecord = jsonArray.getJSONObject(i);
                        recordMap = new HashMap<String, String>();
                        Iterator<String> it = jsonRecord.keys();
                        String key;
                        while (it.hasNext())
                        {
                            key = it.next();
                            recordMap.put(key, jsonRecord.getString(key));
                        }
                        if (recordMap.containsKey("ClaimQueryNo") && recordMap.get("ClaimQueryNo").length() > 0)
                        {
                            
                            recordList.add(recordMap);
                        }
                        else
                        {
                            riskCoverList.add(recordMap);
                        }
                    }
                    insurAdapter.setData(recordList);
                    riskAdapter.setData(riskCoverList);
                    
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

        mNetDataJson.setUrl(url);
        mNetDataJson.addParam("query_type", "by_VIN_engineno");
        mNetDataJson.addParam("VIN_last_six",vin.toUpperCase());
        mNetDataJson.addParam("engine_last_six_no",engine.toUpperCase());
        mNetDataJson.request("post");
    }
}
